#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <stdlib.h>
#include "home_plus.h"

void listener(void * paramsd) }

	struct sockaddr_in cliAddr;
	char line[MAX_MSG];
	char reply[MAX_MSG];
	int i, rc=0;
	int client_local; 
	int addr_len;       

	client_local = *((int *)paramsd); /* store socket descriptor */
	addr_len = sizeof(cliAddr); /* store value of size of sockaddr_in */

	/* get clients name and store in cliAddr */
	getpeername(client_local, (struct sockaddr*)&cliAddr, &addr_len);	
	/* reset line */
	memset(line, 0, MAX_MSG);

	time(&startTime);	/* get start time */
	/* now read lines from the client socket */
	while(1)  /* loop - read from socket */
	{
		rc = read(client_local, line, sizeof(line)-1);
		if(rc > 0)
		{
			if(strncmp(line, time_msg1, 6)==0 || strncmp(line, time_msg2, 6)==0)
			{
				time(&currtime);                  /* get current time */
				ttime = difftime(currtime, startTime);
				//    strcpy(reply, ctime(&currtime));  /* copy into string */
				sprintf(reply, "%lf", ttime);
				if(-1== send(client_local, reply, strlen(reply)+1, 0)) /* return current time to client */
					break;
			}
			else
			{
				break;
			}
		}
		else
			break;
		//	printf("sent data : %s\nsize : %d\n", reply, i);
	//	sleep(1);
	} /* while() */
	number--;	
	printf("Client num : %d\n", number);
	close(client_local);        
	return;
} /* listener() */

int main (void) {

    int client;  /* client socket */
    int rc;   
    struct sockaddr_in local_addr, serv_addr;
    struct hostent * host;
    char message[6] = {'/','T','I','M','E','\n'};
    char message_2[6] = { '/','Q','U','I','T','\n'};
    int sel=0;

    char date[25];
   
    if(argc < 2) {
        printf("usage: %s <server>\n",argv[0]);
        exit(-1);
    }

    /* get host address from specified server name */
    host = gethostbyname(argv[1]);
  
    if (host == NULL) 
    {
        printf("%s: unknown host '%s'\n",argv[0],argv[1]);
        exit(-1);
    }

    /* now fill in sockaddr_in for remote address */
    serv_addr.sin_family = host->h_addrtype;
    /* get first address in host, copy to serv_addr */
    memcpy((char *) &serv_addr.sin_addr.s_addr, host->h_addr_list[0], host->h_length);
    serv_addr.sin_port = htons(SERVER_PORT);
    memset(serv_addr.sin_zero, 0, 8);

    /* create local stream socket */
    client = socket(PF_INET, SOCK_STREAM, 0);
    if (client < 0) {
        perror("cannot open socket ");
        exit(-1);
    }

    /* bind local socket to any port number */
    local_addr.sin_family = AF_INET;
    local_addr.sin_addr.s_addr = htonl(INADDR_ANY);
    local_addr.sin_port = htons(0);
    memset(local_addr.sin_zero, 0, 8);

   
    if (rc < 0) 
    {
        printf("%s: cannot bind port TCP %u\n",argv[0],SERVER_PORT);
        perror("error ");
        exit(1);
    }
   
    /* connect to server */
    rc = connect(client, (struct sockaddr *) &serv_addr, sizeof(serv_addr));
    if (rc < 0) 
    {
        perror("cannot connect ");
        exit(1);
    }

    /* now send /TIME */
       
//	rc = send(client, message, strlen(message)  , 0 );
       
        while(1){ 
 		printf("1번은 접속시간 2번은 종료 :");
		scanf(" %d",&sel);
		if( sel ==1){//접속 시간이면
 			rc = send(client, message, strlen(message) + 1,0);	
			printf("connetc time:");
 
   	
			 rc= read(client,date,25);

  
          		 if (rc < 0) 
   	        	 {
      	          	  perror("cannot send data "); 
      	      	   	 close(client);
             	   	 exit(-1);
  	        	 }
                		 printf(date);
                		 printf("\n");
       		}else{ //종료면
		       	rc =send(client, message_2, strlen(message_2) +1, 0);
          		printf("정상종료 되었습니다 .\n");
	       		 break;
       	        }
       }  

    close(client);	
    return 0;
  
} /* main() */

